﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NomClasses;

namespace NomCafe
{
    public partial class frmTableManager : DevComponents.DotNetBar.Office2007RibbonForm
    {
        string codeTable;
        public frmTableManager()
        {
            InitializeComponent();
        }

        private void frmTableManager_Load(object sender, EventArgs e)
        {
            getDataTable();
            getDataCbbArea();
        }

        private void getDataTable()
        {
            listViewTable.Items.Clear();
            // Lấy danh sách bàn cho listViewTable
            NomClasses.NomDataContext nc = new NomClasses.NomDataContext();
            System.Data.Linq.Table<NomClasses.nom_table> tableList = nc.nom_tables;
            foreach (NomClasses.nom_table table in tableList)
            {
                string[] row = {table.nom_area.name , table.description };
                listViewTable.Items.Add(table.code).SubItems.AddRange(row);
            }
        }

        private void getDataCbbArea()
        {
            NomClasses.NomDataContext nc = new NomClasses.NomDataContext();
            System.Data.Linq.Table<NomClasses.nom_area> area = nc.nom_areas;
            cbbArea.DataSource = area.ToArray();
            cbbArea.DisplayMember = "name";
            cbbArea.ValueMember = "id";
        }

        private void listViewTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnDelete.Enabled = true;
            btnEdit.Enabled = true;
            btnAccept.Enabled = false;

            codeTable = "";
            for (int i = 0; i < listViewTable.Items.Count; i++)
            {
                if (listViewTable.Items[i].Selected)
                {
                    codeTable = listViewTable.Items[i].SubItems[0].Text;
                    txtCodeTable.Text = codeTable;
                    cbbArea.Text = listViewTable.Items[i].SubItems[1].Text;
                    txtNoteTable.Text = listViewTable.Items[i].SubItems[2].Text;
                }
            }
        }

        private void btnRenew_Click(object sender, EventArgs e)
        {
            getDataTable();
            getDataCbbArea();

            txtCodeTable.Text = "";
            txtNoteTable.Text = "";

            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            btnAccept.Enabled = true;
            btnRenew.Enabled = true;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            bool flag = false;
            try
            {
                if (txtCodeTable.Text != "")
                {
                    NomDataContext nc = new NomDataContext();
                    var tableList = from table in nc.nom_tables
                                   select table;
                    foreach (var i in tableList)
                    {
                        if (txtCodeTable.Text.Trim().ToLower().Equals(i.code.Trim().ToLower()))
                        {
                            flag = true;
                        }
                    }
                    if (flag == true)
                    {
                        MessageBox.Show(languageManager.ALREADY_TABLE_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        nom_table table = new nom_table();
                        table.area_id = Convert.ToInt16(cbbArea.SelectedValue);
                        table.code = txtCodeTable.Text;
                        table.description = txtNoteTable.Text;
                        nc.nom_tables.InsertOnSubmit(table);
                        nc.SubmitChanges();

                        getDataTable();

                        txtCodeTable.Text = "";
                        txtNoteTable.Text = "";
                        txtCodeTable.Focus();
                    }
                }
                else
                {
                    MessageBox.Show(languageManager.INSERT_TABLE_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            bool flag = false;
            try
            {
                NomDataContext nc = new NomDataContext();
                var tableList = from table in nc.nom_tables
                                where table.code != codeTable
                                select table;
                foreach (var i in tableList)
                {
                    if (txtCodeTable.Text.Trim().ToLower().Equals(i.code.Trim().ToLower()))
                    {
                        flag = true;
                    }
                }
                if (flag == true)
                {
                    MessageBox.Show(languageManager.ALREADY_TABLE_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {                    
                    nom_table table = nc.nom_tables.Single(i => i.code == codeTable);
                    table.area_id = Convert.ToInt16(cbbArea.SelectedValue);
                    table.code = txtCodeTable.Text;
                    table.description = txtNoteTable.Text;
                    nc.SubmitChanges();

                    btnRenew_Click(sender, e);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                NomDataContext nc = new NomDataContext();
                nom_table table = nc.nom_tables.Single(i => i.code == codeTable);
                nc.nom_tables.DeleteOnSubmit(table);
                nc.SubmitChanges();

                btnRenew_Click(sender, e);
            }
            catch (Exception)
            {
                MessageBox.Show(languageManager.DELETE_TABLE_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
